package com.wzdigit.wms.wms.controller.stockIn;

import com.wzdigit.framework.model.response.ErrorEntity;
import com.wzdigit.wms.basic.client.Pager;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.Queryer;
import com.wzdigit.wms.basic.client.request.asn.ReceiveReportDtlRequest;
import com.wzdigit.wms.basic.client.request.asn.ReceiveReportSelectRequest;
import com.wzdigit.wms.basic.client.request.report.SettlementReportSearchRequest;
import com.wzdigit.wms.basic.client.response.asnHeader.ReceiveReportDtlExt;
import com.wzdigit.wms.basic.client.response.asnHeader.ReceiveReportSNExt;
import com.wzdigit.wms.basic.client.response.asnHeader.ReceiveReportZhuExt;
import com.wzdigit.wms.basic.client.response.report.SettlementReportExt;
import com.wzdigit.wms.wms.constant.WmsWareConstant;
import com.wzdigit.wms.wms.service.core.SettlementReportService;
import com.wzdigit.wms.wms.service.order.AsnService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@Api(tags = "报表中心")
@RequestMapping(value = WmsWareConstant.API_VERSION)
public class ReportController {

    private final static String REQUEST_MODEL = "/Reports";
    
    @Autowired
    private SettlementReportService settlementReportService;
    
    @Autowired
    private AsnService asnService;
    
	/**
	 * 收货报表主数据
	 * @param search
	 * @param pager
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 */
	@ApiOperation(value = "收货报表主数据", notes = "收货报表主数据")
	@ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
	@RequestMapping(value = REQUEST_MODEL + "/receiveReport", method = RequestMethod.GET)
	public PagerInfo<List<ReceiveReportZhuExt>> searchReceiveReport(
			ReceiveReportSelectRequest search, Pager<?> pager,
			HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
		try {
			return asnService.searchReceiveReport(search, pager);
		} catch (Exception ex) {
			servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			throw ex;
		}
	}
    
	/**
	 * 收货报表明细数据
	 * @param search
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 */
	@ApiOperation(value = "收货报表明细数据", notes = "收货报表明细数据")
	@ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
	@RequestMapping(value = REQUEST_MODEL + "/receiveReportDtl", method = RequestMethod.GET)
	public List<ReceiveReportDtlExt> searchReceiveReportDtl(
			ReceiveReportDtlRequest search,
			HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
		try {
			return asnService.searchReceiveReportDtl(search);
		} catch (Exception ex) {
			servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			throw ex;
		}
	}
    
	/**
	 * 收货单SN数据
	 * @param search
	 * @param pager
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 */
	@ApiOperation(value = "收货单SN数据", notes = "收货单SN数据")
	@ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
	@RequestMapping(value = REQUEST_MODEL + "/receiveReportSN", method = RequestMethod.GET)
	public PagerInfo<List<ReceiveReportSNExt>> searchReceiveReportSN(
			ReceiveReportDtlRequest search, Pager<?> pager,
			HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
		try {
			return asnService.searchReceiveReportSN(search, pager);
		} catch (Exception ex) {
			servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			throw ex;
		}
	}
    
	/**
	 * 导出收货单
	 * @param search
	 * @param servletRequest
	 * @param servletResponse
	 * @throws Exception
	 */
	@ApiOperation(value = "导出收货单", notes = "导出收货单")
	@ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
	@RequestMapping(value = REQUEST_MODEL + "/exportReceiveReport", method = RequestMethod.GET)
	public void exportReceiveReport(
			ReceiveReportSelectRequest search, 
			HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception {
		try {
//			asnService.exportReceiveReport(search, servletRequest, servletResponse);
//			asnService.exportReceiveReportNew(search, servletRequest, servletResponse);
			asnService.exportReceiveReportNew3(search, servletRequest, servletResponse);
		} catch (Exception e) {
			servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			throw e;
		} 
	}
	
	/**
	 * 导出收货单SN信息
	 * @param search
	 * @param servletRequest
	 * @param servletResponse
	 * @throws Exception
	 */
	@ApiOperation(value = "导出收货单SN信息", notes = "导出收货单SN信息")
	@ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
	@RequestMapping(value = REQUEST_MODEL + "/exportReceiveReportSN", method = RequestMethod.GET)
	public void exportReceiveReportSN(
			ReceiveReportDtlRequest search, 
			HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception {
		try {
			asnService.exportReceiveReportSN(search, servletRequest, servletResponse);
		} catch (Exception e) {
			servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			throw e;
		}
	}

	@ApiOperation(value = "入库-交收报表", notes = "入库-交收报表")
	@ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
	@RequestMapping(value = REQUEST_MODEL + "/settlement", method = RequestMethod.GET)
	public PagerInfo<List<SettlementReportExt>> settlementList(
			SettlementReportSearchRequest search, Pager<?> pager,
			HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
		Queryer<SettlementReportSearchRequest> queryer = new Queryer<SettlementReportSearchRequest>();
		queryer.setPage(pager);
		queryer.setRequest(search);
		try {
			return settlementReportService.settlementList(queryer);
		} catch (Exception ex) {
			servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			throw ex;
		}
	}

	/**
	 * 导出交收报表信息
	 * @param search
	 * @param servletRequest
	 * @param servletResponse
	 */
	@ApiOperation(value = "导出交收报表信息", notes = "导出交收报表信息")
	@ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
	@RequestMapping(value = REQUEST_MODEL + "/settlement/export", method = RequestMethod.GET)
	public void exportSettlement(
			SettlementReportSearchRequest search,
			HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
		settlementReportService.exportSettlement(search, servletRequest, servletResponse);
	}

	@ApiOperation(value = "入库-交收日报表", notes = "入库-交收日报表")
	@ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
	@RequestMapping(value = REQUEST_MODEL + "/settlementDay", method = RequestMethod.GET)
	public PagerInfo<List<SettlementReportExt>> settlementListDay(
			SettlementReportSearchRequest search, Pager<?> pager,
			HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
		Queryer<SettlementReportSearchRequest> queryer = new Queryer<SettlementReportSearchRequest>();
		queryer.setPage(pager);
		queryer.setRequest(search);
		try {
			return settlementReportService.settlementListDay(queryer);
		} catch (Exception ex) {
			servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			throw ex;
		}
	}

	/**
	 * 导出交收报表信息
	 * @param search
	 * @param servletRequest
	 * @param servletResponse
	 */
	@ApiOperation(value = "导出交收日报表信息", notes = "导出交收日报表信息")
	@ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
	@RequestMapping(value = REQUEST_MODEL + "/settlementDay/export", method = RequestMethod.GET)
	public void exportSettlementDay(
			SettlementReportSearchRequest search,
			HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
		settlementReportService.exportSettlementDay(search, servletRequest, servletResponse);
	}
}